package cn.neusoft.yuejian307.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.neusoft.yuejian307.R;
import cn.neusoft.yuejian307.activity.TrainDetail;
import cn.neusoft.yuejian307.bean.Train;

import static cn.neusoft.yuejian307.GloableVirable.IMG_URL;


public class TrainAdapter extends  RecyclerView.Adapter< RecyclerView.ViewHolder> {
    private Context mContext;
    private List<Train> mList;
    public LayoutInflater mLayoutInflater;

    public TrainAdapter(Context mContext, List<Train> List) {
        this.mContext = mContext;
        this.mList = List;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_train, parent, false);
        TrainAdapter.ViewHolder v = new TrainAdapter.ViewHolder(view);

        return v;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Train club = (Train) mList.get(position);
        if (null == club)
            return;
        TrainAdapter.ViewHolder viewHolder = (TrainAdapter.ViewHolder) holder;
        viewHolder.name.setText(club.getName());
        viewHolder.intro.setText(club.getTrain_price());
        if(club.getTrain_picture()!=null) {
            Picasso.with(mContext).load(IMG_URL+club.getTrain_picture()).into(viewHolder.img_club);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, TrainDetail.class);
                Bundle bundle = new Bundle();
                bundle.putInt("1",club.getId());
                bundle.putString("2",IMG_URL+club.getTrain_picture());
                bundle.putString("3",club.getTrain_intro());
                bundle.putString("4",club.getTrain_must());
                bundle.putString("5",club.getTrain_time());
                bundle.putString("6",club.getTrain_address());
                bundle.putString("7",club.getTrain_price()+"");
                bundle.putString("8",club.getName());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name,intro;
        ImageView img_club;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            intro = (TextView) itemView.findViewById(R.id.intro);
            img_club = (ImageView) itemView.findViewById(R.id.img_club);

        }
    }
}
